var Xunlei = Xunlei || {};
Xunlei.thunderBatchTask = function (num) {
    BatchTasker.BeginBatch();
    allBox = document.getElementsByName('CopyAddr' + num + '');
    for (var k = 0; k < allBox.length; k++) {
        if (allBox[k].checked == true) {
            BatchTasker.AddTask(ThunderEncode(allBox[k].value));
        }

    }
    BatchTasker.EndBatch();
};

Xunlei.xunbotask = function (linkObj) {
    var urls = linkObj.getAttribute("thunderHref");
    //encode
    urls = ThunderEncode(urls);
    BatchTasker.BeginBatch();
    BatchTasker.AddTask(urls);
    BatchTasker.EndBatch();
};

var Delegate = {
    create: function (obj, func){
        var f = function()	{
            var target = arguments.callee.target;
            var func = arguments.callee.func;
            return func.apply(target, arguments);
        }
        f.target = obj;
        f.func = func;
        return f;
    }
};

var My_Thunder = My_Thunder|| {}
My_Thunder.__thunder  =  window.ActiveXObject ? new ActiveXObject("ThunderAgent.Agent.1") : null;
My_Thunder.request =  function (zhi,paras){
    var paraObj = {};
    for (i=0; j=zhi[i]; i++){
        paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if(typeof(returnValue)=="undefined"){
        return "";
    }else{
        return returnValue;
    }
};
My_Thunder.d = function(obj){
    var obj_value = document.getElementById(obj).value;
    var urlcs = obj_value.split("&");
    var name= this.request(urlcs,"mc");
    var url= this.request(urlcs,"xzurl");
    var cid= this.request(urlcs,"cid");
    var pmc;
    url="http://bt.2tu.cc/" + cid + "/"+name;
    if (cid==""){
        url= this.request(urlcs,"xzurl");
    }

    if (obj_value.indexOf("[BT]")==0){
        url="http://bt.2tu.cc/"+cid.slice(0,2)+"/"+cid.slice(cid.length-2,cid.length)+"/"+cid+".torrent";
        pmc = "xunbocc：" + name;
    }else if(obj_value.indexOf("[EMULE]")==0){
        alert("电驴下载制作中");
        return;
    }else{
        if (this.request(urlcs,"xzurl")!="") {
            url = this.request(urlcs,"xzurl");
        }else{
            pmc = "XUNBOCC：" + name;
        }
    }
    name = name.replace(/\\|\/|:|\*|\?|"|<|>|\|/g," ");
    url=ThunderEncode(url);
    this.download(cid,  url, name, "", "");

};
 My_Thunder.download = function(cid, url, refer, name, stat){
    try{
        this.addTask(cid, url, refer, name, stat);
        this.commitTasks();
    }catch(e){
        alert(e.message);
    }
};
My_Thunder.addTask = function(cid, url, refer, name, stat){
    var _addTask = [
        Delegate.create(this, function(){My_Thunder.__thunder.AddTask4(url, name, "", name, refer, -1, 0, -1, "", cid, stat);}),
        Delegate.create(this, function(){My_Thunder.__thunder.AddTask3(url, name, "", name, refer, -1, 0, -1, "", cid);}),
        Delegate.create(this, function(){My_Thunder.__thunder.AddTask2(url, name, "", name, refer, -1, 0, -1, "");}),
        Delegate.create(this, function(){My_Thunder.__thunder.AddTask(url, name, "", name, refer, -1, 0, -1);})
    ];
    for (var i=0; i<_addTask.length; i++){
        try{
            _addTask[i]();
            return;
        }catch(e){
        }
    }
    throw "不支持此方法，请安装最新的迅雷客户端";
};
My_Thunder.commitTasks = function(){
    var _commitTasks = [
        Delegate.create(this, function(){My_Thunder.__thunder.CommitTasks2(1);}),
        Delegate.create(this, function(){My_Thunder.__thunder.CommitTasks();})
    ];
    for (var i=0; i<_commitTasks.length; i++){
        try{
            _commitTasks[i]();
            return;
        }catch(e){
        }
    }
    throw "不支持此方法，请安装最新的迅雷客户端";
}